package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.OrderDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private OrderDetailService orderDetailService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    @GetMapping("/userPage")
    public R<Page<OrderDto>> page(int page, int pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrderDto> dtoPage=new Page<>();
        orderService.page(pageInfo);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getCheckoutTime);
        lqw.eq(Orders::getUserId,currentId);
        List<Orders> orderList = orderService.list(lqw);
        List<OrderDto> orderDtos=orderList.stream().map((item)->{
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(item,orderDto);
            //获取order表的每一个数据
            Long orderId = item.getId();
            LambdaQueryWrapper<OrderDetail> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(OrderDetail::getOrderId,orderId);
            List<OrderDetail> orderDetails = orderDetailService.list(lqw2);
            int sum=0;
            for (OrderDetail orderDetail : orderDetails) {
                sum+=orderDetail.getNumber();
            }
            orderDto.setItemNum(sum);
            return orderDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(orderDtos);
        return R.success(dtoPage);
    }

    /**
     * 订单查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrderDto>> backendPage(int page, int pageSize, Long number, String beginTime,String endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrderDto> dtoPage=new Page<>();
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(number!=null,Orders::getId,number);
        //根据日期区间查询
        lqw.between((StringUtils.isNotBlank(beginTime))&&(StringUtils.isNotBlank(endTime))
                ,Orders::getOrderTime,beginTime,endTime);
        orderService.page(pageInfo, lqw);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrderDto> orderDtos=records.stream().map((item)->{
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(item,orderDto);
            Long addressBookId = item.getAddressBookId();
            LambdaQueryWrapper<AddressBook> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(AddressBook::getId,addressBookId);
            AddressBook address = addressService.getOne(wrapper);
            orderDto.setAddress(address.getDetail());
            return orderDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(orderDtos);
        return R.success(dtoPage);
    }
    @PutMapping
    public R<String> status(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("订单状态修改成功");
    }
}
